package com.wantupai.mvp.framework.base.adapter.viewholder;

import android.content.Context;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;


/**
 * @author :Created by zhangqing on 2018/5/15 15:51.
 * @description :万能的viewHolder
 * @email :1423118197@qq.com
 * @classpath : cn.doolii.mvp.framework.base.adapter.viewholder.SmartViewHolder
 */
public class SmartViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> mViews;

    private View mConvertView;


    private boolean isListView = false;

    protected int position = 0;

    private SmartViewHolder(Context context, View itemView) {
        super(itemView);
        mConvertView = itemView;
        mViews = new SparseArray<View>();
        isListView = false;
    }

    private SmartViewHolder(Context context, View itemView, ViewGroup parent, int position) {
        super(itemView);
        mConvertView = itemView;
        mViews = new SparseArray<View>();
        mConvertView.setTag(this);
        isListView = true;
    }

    public static SmartViewHolder get(Context context, ViewGroup parent, int layoutId) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        SmartViewHolder holder = new SmartViewHolder(context, itemView);
        return holder;
    }

    public static SmartViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
            SmartViewHolder smartViewHolder = new SmartViewHolder(context, convertView, parent, position);
            smartViewHolder.position = position;
            return smartViewHolder;
        } else {
            SmartViewHolder smartViewHolder = (SmartViewHolder) convertView.getTag();
            smartViewHolder.position = position;
            return smartViewHolder;
        }
    }

    public int getItemPosition() {
        return isListView ? position : getAdapterPosition();
    }

    public View getConvertView() {
        return mConvertView;
    }


    public <T extends View> T findViewById(@IdRes @NonNull int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            if (view == null) {
                throw new NullPointerException("can not find view By id" + view);
            }
            mViews.put(viewId, view);
        }
        return (T) view;
    }


    public SmartViewHolder setSelected(boolean selected, @IdRes int... ids) {
        if (ids == null || ids.length == 0) {
            throw new NullPointerException("提供的viewId可变参数不能为空");
        }
        for (Integer id : ids) {
            findViewById(id).setSelected(selected);
        }
        return this;
    }


    public SmartViewHolder setFlags(@IdRes int id, int flag) {
        TextView textView = findViewById(id);
        if (textView != null) {
            textView.getPaint().setFlags(flag);
        }
        return this;
    }

    public SmartViewHolder setText(@IdRes int id, CharSequence sequence) {
        TextView textView = findViewById(id);
        if (sequence != null && sequence.length() > 0) {
            textView.setText(String.valueOf(sequence));
            textView.setVisibility(View.VISIBLE);
        } else {
            textView.setVisibility(View.GONE);
        }
        return this;
    }

    public <T extends ListAdapter> SmartViewHolder setAdapter(@IdRes int id, T adapter) {
        AbsListView absListView = findViewById(id);
        absListView.setAdapter(adapter);
        int count = adapter.getCount();
        if (absListView instanceof GridView) {
            GridView gridView = (GridView) absListView;
         /*   if (count == 4)
            {
                gridView.setNumColumns(2);
            }*/
        }
        absListView.setVisibility(count == 0 ? View.GONE : View.VISIBLE);
        return this;
    }

    public SmartViewHolder setText(@IdRes int id, CharSequence sequence, int maxLenght) {
        TextView textView = findViewById(id);
        if (sequence != null && sequence.length() > 0) {
            String str = sequence.toString();
            int length = sequence.length();
            if (length >= maxLenght) {
                str = sequence.subSequence(0, maxLenght).toString();
            }
            textView.setText(String.valueOf(str));
            textView.setVisibility(View.VISIBLE);
        } else {
            textView.setVisibility(View.GONE);
        }
        return this;
    }

    public SmartViewHolder setText(@IdRes int id, CharSequence sequence, int maxLenght, String prefex) {
        TextView textView = findViewById(id);
        if (sequence != null && sequence.length() > 0) {
            String str = sequence.toString();
            int length = sequence.length();
            if (length >= maxLenght) {
                str = sequence.subSequence(0, maxLenght).toString() + prefex;
            }
            textView.setText(String.valueOf(str));
            textView.setVisibility(View.VISIBLE);
        } else {
            textView.setVisibility(View.GONE);
        }
        return this;
    }

    public SmartViewHolder setBackground(@IdRes int id, @ColorRes int colorId) {
        View viewById = findViewById(id);
        viewById.setBackgroundColor(ContextCompat.getColor(viewById.getContext(), colorId));
        return this;
    }

    public SmartViewHolder setTextSize(@IdRes int id, float textSize) {
        TextView textView = findViewById(id);
        if (textSize <= 0) {
            throw new IllegalArgumentException("textSize can not below 1" + textSize);
        }
        textView.setTextSize(textSize);
        return this;
    }

    public SmartViewHolder setText(@IdRes int id, @StringRes int stringRes) {
        TextView textView = findViewById(id);
        textView.setText(stringRes);
        return this;
    }

    public SmartViewHolder setTextColor(@IdRes int id, @ColorRes int colorId) {
        TextView textView = findViewById(id);
        textView.setTextColor(ContextCompat.getColor(textView.getContext(), colorId));
        return this;
    }


    public SmartViewHolder setVisible(int Visibility, @IdRes int... ids) {
        for (Integer viewId : ids) {
            View view = findViewById(viewId);
            view.setVisibility(Visibility);
        }
        return this;
    }

    public SmartViewHolder setVisible(CharSequence charSequence, @IdRes int... ids) {
        for (Integer viewId : ids) {
            setVisible(TextUtils.isEmpty(charSequence) ? View.GONE : View.VISIBLE, viewId);
        }
        return this;
    }


    public SmartViewHolder setImageResource(@IdRes int viewId, @DrawableRes int drawableResId) {
        ImageView imageView = findViewById(viewId);
        imageView.setImageResource(drawableResId);
        return this;
    }

    public SmartViewHolder setBackgroundResource(@IdRes int viewId, @DrawableRes int drawableResId) {
        findViewById(viewId).setBackgroundResource(drawableResId);
        return this;
    }

    public void setOnClick(View.OnClickListener onClickListener, @IdRes int... ids) {
        if (onClickListener == null) {
            throw new NullPointerException("onClickListener can not is null");
        }
        for (Integer id : ids) {
            findViewById(id).setOnClickListener(v -> onClickListener.onClick(v));
        }
    }
}
